package com.yu.crm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.whyxzz.arcface.ArcFace;
import com.yu.crm.domain.pojo.CallerReservationPeople;
import com.yu.crm.domain.pojo.CrmBlacklistUser;
import com.yu.crm.domain.pojo.CrmUser;
import com.yu.crm.mapper.CrmBlacklistUserMapper;
import com.yu.crm.service.CrmBlacklistUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxzz.common.helper.DataHelpers;
import com.yxzz.common.helper.DateHelpers;
import com.yxzz.common.helper.ExceptionHelpers;
import com.yxzz.common.helper.valid.ValidatorHelpers;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 黑名单用户表 服务实现类
 * </p>
 *
 * @author ct
 * @since 2020-07-01
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class CrmBlacklistUserServiceImpl extends ServiceImpl<CrmBlacklistUserMapper, CrmBlacklistUser> implements CrmBlacklistUserService {


    @Override
    public IPage<CrmBlacklistUser> list(IPage<CrmBlacklistUser> page, CrmBlacklistUser queryInfo) {
        LambdaQueryWrapper<CrmBlacklistUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (DataHelpers.isNotEmpty(queryInfo.getUsername())) {
            lambdaQueryWrapper.like(CrmBlacklistUser::getUsername, queryInfo.getUsername());
        }
        lambdaQueryWrapper.orderByDesc(CrmBlacklistUser::getGmtCreate);
        lambdaQueryWrapper.eq(CrmBlacklistUser::getType, queryInfo.getType());
        lambdaQueryWrapper.orderByAsc(CrmBlacklistUser::getUsername);
        return super.page(page, lambdaQueryWrapper);
    }

    @Override
    public String isBlack(byte[] bytes, ArcFace arcFace) {
        QueryWrapper<CrmBlacklistUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_deleted",0);
        List<CrmBlacklistUser> CrmBlacklistUsers = baseMapper.selectList(queryWrapper);
        for (CrmBlacklistUser crmBlacklistUser:CrmBlacklistUsers
        ) {
            if (arcFace.compareFaceFeature(bytes, crmBlacklistUser.getCertificateEigen())) {
                return crmBlacklistUser.getId().toString();
            }
        }
        return "";
    }

    @Override
    public boolean saveOrUpdate(CrmBlacklistUser entity) {
        if (DataHelpers.isEmpty(entity.getId())) {
            entity.setGmtCreate(DateHelpers.now());
            entity.setGmtModified(DateHelpers.now());
            return ExceptionHelpers.checkData(super.save(entity), "新增失败");
        } else {
            entity.setGmtModified(DateHelpers.now());
            return ExceptionHelpers.checkData(super.updateById(entity), "更新失败");
        }
    }
}
